package com.gogowan.agroenterprise.supply;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.SeekSDInfo;
import com.gogowan.agroenterprise.model.SeekSDResultInfo;
import com.gogowan.agroenterprise.utils.Utils;

public class SearchActivity extends BaseActivity{
	
	private Spinner productType;
	private EditText keywords;
	private Button searchBtn;
	private int selected = 0;
	
	private SharedPreferences preferences;
	private long enterpriseId;
	private ProgressDialog progressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.supply_search);
		
		productType = (Spinner)findViewById(R.id.search_product_type);
		keywords = (EditText)findViewById(R.id.search_keywords);
		searchBtn = (Button)findViewById(R.id.search_btn);
		
		preferences = getSharedPreferences("LoginINfo", MODE_PRIVATE);
		enterpriseId = preferences.getLong("enterpriseid", 0);
		
		String [] m1 = new String[]{"供应","求购"};
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(SearchActivity.this,android.R.layout.simple_spinner_item,m1);
		//设置下拉列表的风格
		 adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		 //将adapter 添加到spinner中
		 productType.setAdapter(adapter1);
		 
		 productType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0) {
					selected = 0;
				} else {
					selected = 1;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			
			}
		});
		 
		 searchBtn.setOnClickListener(searchCallback);
	}
	
	/**
	 * 搜索按钮点击事件
	 */
	public Button.OnClickListener searchCallback = new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			SeekSDInfo sdInfo = new SeekSDInfo();
			sdInfo.setTypeCode(selected);
			sdInfo.setKeyWords(keywords.getText().toString().trim());
			sdInfo.setEnterpriseID(enterpriseId);
			sdInfo.setSdID(0);
			if (selected == 0) {
				progressDialog = ProgressDialog.show(SearchActivity.this,
						getString(R.string.alert_title),
						"正在搜索相关供应信息", true, false);
			} else {
				progressDialog = ProgressDialog.show(SearchActivity.this,
					getString(R.string.alert_title),
					"正在搜索相关求购信息", true, false);
			}
			SearchAsyn searchAsyn = new SearchAsyn();
			searchAsyn.execute(sdInfo);
		}
		
	};
	
	/**
	 * 搜索异步操作
	 * @author Maniac
	 * 2012.12.17
	 */
	private class SearchAsyn extends MyAsyncTask{

		@Override
		protected void onPostExecute(Object res) {
			if (res == null || res.equals("")) {
				Toast.makeText(SearchActivity.this, getString(R.string.server_error), Toast.LENGTH_LONG).show();
			} else {
				
				Message message = (Message) Utils.fromJSON((String) res, Message.class);
				String string = Utils.getMessageContent(message);
				if (string == null || string.equals("")) {
					Toast.makeText(SearchActivity.this, getString(R.string.analyse_error), Toast.LENGTH_LONG).show();
				} else {
					SeekSDResultInfo resultInfo = (SeekSDResultInfo)Utils.fromJSON(string, SeekSDResultInfo.class);
					int result = resultInfo.getResult();
					if (result == 8010) {
						Intent intent = new Intent(SearchActivity.this,SearchResultListActivity.class);
						intent.putExtra("ResultList", string);
						intent.putExtra("sdType", selected);
						intent.putExtra("keywords", keywords.getText().toString().trim());
						startActivity(intent);
						Utils.randomAnim(SearchActivity.this);
					} else {
						Intent intent = new Intent(SearchActivity.this,SearchResultListActivity.class);
						startActivity(intent);
						Utils.randomAnim(SearchActivity.this);

						//Toast.makeText(SearchActivity.this, resultInfo.getResultinfo(), Toast.LENGTH_LONG).show();
					}
				}
			}
			progressDialog.dismiss();
			super.onPostExecute(res);
		}

		@Override
		protected Object doInBackground(Object... params) {
			SeekSDInfo request = (SeekSDInfo)params[0];
			String json = Utils.toJSON(request);
			Message message = new Message(8000, true, json);
			String res = Utils.postCommit(getString(R.string.first_url)
					+ getString(R.string.first_url_page), message);
			return res;
		}
		
	}
}
